package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

//自定义元数据对象处理器
@Component
public class MyMetaObjecthandler implements MetaObjectHandler {
    /**
     * 添加操作 自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        strictInsertFill(metaObject,"createTime",LocalDateTime.class,LocalDateTime.now());
        strictInsertFill(metaObject,"updateTime",LocalDateTime.class,LocalDateTime.now());
        strictInsertFill(metaObject,"createUser",Long.class,BaseContext.getCurrentId());
        strictInsertFill(metaObject,"updateUser",Long.class,BaseContext.getCurrentId());

        /*metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());*/
    }

    /**
     * 修改操作 自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());

    }
}
